package cn.hongwei.eduservice.service.impl;

import cn.hongwei.eduservice.entity.EduTeacher;
import cn.hongwei.eduservice.entity.TeacherQuery;
import cn.hongwei.eduservice.mapper.EduTeacherMapper;
import cn.hongwei.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-22
 */
@Service
@Transactional
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    //注入dao
    @Autowired
    private EduTeacherMapper teacherMapper;

    //查询所有老师
    @Override
    public List<EduTeacher> findAll() {
        QueryWrapper<EduTeacher> queryWrapper=new QueryWrapper<>();
        return teacherMapper.selectList(queryWrapper);
    }

    @Override
    public String deleteById(String id) {
        teacherMapper.deleteById(id);
        return "删除成功";
    }

    @Override
    public Page<EduTeacher> getPageByCondition(Page<EduTeacher> page, TeacherQuery query) {
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();

        System.out.println("query = " + query);
        //判断传入了哪些条件，如果有则使用
        String name = query.getName();
        Integer level = query.getLevel();
        String joinDateBegin = query.getJoinDateBegin();
        String joinDateEnd = query.getJoinDateEnd();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like(EduTeacher::getName , name);
        }
        if(level!=null){
            queryWrapper.eq(EduTeacher::getLevel , level);
        }
        if(!StringUtils.isEmpty(joinDateBegin)){
            queryWrapper.ge(EduTeacher::getJoinDate , joinDateBegin);
        }
        if(!StringUtils.isEmpty(joinDateEnd)){
            queryWrapper.le(EduTeacher::getJoinDate , joinDateEnd);
        }
        return  this.page(page,queryWrapper);


    }
}
